//
//
//  @ Project : AgriMarketSystem
//  @ Date : 18/10/2013
//  @ Author : NTT
//
//
using System;
using System.Collections.Generic;
using System.Linq;
namespace AgriMarketService
{
    public class ArticleComment
    {
        private int commentId;

        public int CommentId
        {
            get { return commentId; }
            set { commentId = value; }
        }

        private int articleId;

        public int ArticleId
        {
            get { return articleId; }
            set { articleId = value; }
        }

        private string content;

        public string Content
        {
            get { return content; }
            set { content = value; }
        }

        private DateTime postedDate;

        public DateTime PostedDate
        {
            get { return postedDate; }
            set { postedDate = value; }
        }

        private string authorId;

        public string AuthorId
        {
            get { return authorId; }
            set { authorId = value; }
        }
        //private UserAccount author;

        //public UserAccount Author
        //{
        //    get { return author; }
        //    set { author = value; }
        //}
        public ArticleComment() { }
        public ArticleComment(int commentId, int articleId, string content, DateTime postedDate, string authorId/*UserAccount author*/)
        {
            this.commentId = commentId;
            this.articleId = articleId;
            this.content = content;
            this.postedDate = postedDate;
            this.authorId = authorId;
            //this.author = author;
        }
        public static int AddNew(ArticleComment newComment)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Sell_Buy_Article newArticle = (from tempNewArticle in db.Tbl_Sell_Buy_Articles
                                               where tempNewArticle.ArticleId == newComment.articleId
                                               select tempNewArticle).FirstOrDefault();
            //if (newArticle == null)
            //{
            //    Tbl_Long_Sell_Buy_Article longArticle = (from tempLongArticle in db.Tbl_Long_Sell_Buy_Articles
            //                                             where tempLongArticle.ArticleId == newComment.articleId
            //                                             select tempLongArticle).FirstOrDefault();
            //    if (longArticle == null)
            //    {
            //        Tbl_Finished_Sell_Buy_Article finishedArticle = (from tempFinishedArticle in db.Tbl_Finished_Sell_Buy_Articles
            //                                                         where tempFinishedArticle.ArticleId == newComment.articleId
            //                                                         select tempFinishedArticle).FirstOrDefault();
            //        if (finishedArticle == null)
            //        {
            //            return -1;//article is not exist
            //        }
            //        else
            //        {
            //            Tbl_Finished_Comment finishedArticleComment = newComment.ToTbl_Finished_Comment();
            //            db.Tbl_Finished_Comments.InsertOnSubmit(finishedArticleComment);
            //            try
            //            {
            //                db.SubmitChanges();
            //                return finishedArticleComment.CommentId;
            //            }
            //            catch (Exception e)
            //            {
            //                return -2;//error
            //            }
            //        }
            //    }
            //    else
            //    {
            //        Tbl_Long_Comment longArticleComment = newComment.ToTbl_Long_Comment();
            //        db.Tbl_Long_Comments.InsertOnSubmit(longArticleComment);
            //        try
            //        {
            //            db.SubmitChanges();
            //            return longArticleComment.CommentId;
            //        }
            //        catch (Exception e)
            //        {
            //            return -2;//error
            //        }
            //    }
            //}
            //else
            //{
            if (newArticle == null)
                return -1;// no exist;
            Tbl_Comment newArticleComment = newComment.ToTbl_Comment();
            db.Tbl_Comments.InsertOnSubmit(newArticleComment);
            try
            {
                db.SubmitChanges();
                return newArticleComment.CommentId;
            }
            catch (Exception e)
            {
                return -2;//error
            }
            //}
        }
        public static int Update(ArticleComment updatedComment)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Comment comment = (from tempComment in db.Tbl_Comments
                                               where tempComment.CommentId == updatedComment.commentId
                                               select tempComment).FirstOrDefault();
            //if (comment == null)
            //{
            //    Tbl_Long_Comment longComment = (from tempLongComment in db.Tbl_Long_Comments
            //                           where tempLongComment.CommentId == updatedComment.commentId
            //                           select tempLongComment).FirstOrDefault();
            //    if (longComment == null)
            //    {
            //        Tbl_Finished_Comment finishedComment = (from tempFinishedComment in db.Tbl_Finished_Comments
            //                                                where tempFinishedComment.CommentId == updatedComment.commentId
            //                                                select tempFinishedComment).FirstOrDefault();
            //        if (finishedComment == null)
            //        {
            //            return -1;//article is not exist
            //        }
            //        else
            //        {
            //            finishedComment.Content = updatedComment.content;
            //            finishedComment.PostedDate = updatedComment.postedDate;
            //        }
            //    }
            //    else
            //    {
            //        longComment.Content = updatedComment.content;
            //        longComment.PostedDate = updatedComment.postedDate;
            //    }
            //}
            //else
            //{
                //comment.Content = updatedComment.content;
                //comment.PostedDate = updatedComment.postedDate;
            //}
            if (comment == null)
                return -1;//no exist
            comment.Content = updatedComment.content;
            comment.PostedDate = updatedComment.postedDate;
            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch (Exception e)
            {
                return 1;//error
            }
        }
        public static int DeleteByArticleCommentId(int deletedArticleCommentId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            Tbl_Comment deletedArticleComment = (from tempDeletedArticletComment in db.Tbl_Comments
                               where tempDeletedArticletComment.CommentId == deletedArticleCommentId
                               select tempDeletedArticletComment).FirstOrDefault();
            //if (deletedArticleComment == null)
            //{
            //    Tbl_Finished_Comment deletedFinishedArticleComment = (from tempDeletedFinishedArticletComment in db.Tbl_Finished_Comments
            //                                                          where tempDeletedFinishedArticletComment.CommentId == deletedArticleCommentId
            //                                                          select tempDeletedFinishedArticletComment).FirstOrDefault();
            //    if (deletedFinishedArticleComment == null)
            //    {
            //        Tbl_Long_Comment deletedLongArticleComment = (from tempDeletedLongArticletComment in db.Tbl_Long_Comments
            //                                                              where tempDeletedLongArticletComment.CommentId == deletedArticleCommentId
            //                                                              select tempDeletedLongArticletComment).FirstOrDefault();
            //        if (deletedLongArticleComment == null)
            //            return -1;//no exist;
            //        else
            //        {
            //            db.Tbl_Long_Comments.DeleteOnSubmit(deletedLongArticleComment);
            //        }
            //    }
            //    else
            //    {
            //        db.Tbl_Finished_Comments.DeleteOnSubmit(deletedFinishedArticleComment);
            //    }
            //}
            //else
            //{
            //    db.Tbl_Comments.DeleteOnSubmit(deletedArticleComment);
            //}
            if(deletedArticleComment==null)
                return -1;//no exist;
            db.Tbl_Comments.DeleteOnSubmit(deletedArticleComment);
            try
            {
                db.SubmitChanges();
                return 0;
            }
            catch (Exception e)
            {
                return 1;//error
            }
        }
        public static List<ArticleComment> GetAllCommentByArticleId(int articleId)
        {
            List<ArticleComment> articleComments=null;
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            articleComments = (from tempArticleComment in db.Tbl_Comments
                    where tempArticleComment.ArticleId==articleId
                    select new ArticleComment(tempArticleComment.ArticleId,
                        tempArticleComment.ArticleId,
                        tempArticleComment.Content,
                        tempArticleComment.PostedDate,
                        tempArticleComment.AuthorId)).ToList();
            //if (articleComments.Count == 0)
            //{
            //    articleComments = (from tempArticleComment in db.Tbl_Finished_Comments
            //                       where tempArticleComment.ArticleId == articleId
            //                       select new ArticleComment(tempArticleComment.ArticleId,
            //                           tempArticleComment.ArticleId,
            //                           tempArticleComment.Content,
            //                           tempArticleComment.PostedDate,
            //                           tempArticleComment.AuthorId)).ToList();
            //    if (articleComments.Count == 0)
            //    {
            //        articleComments = (from tempArticleComment in db.Tbl_Long_Comments
            //                           where tempArticleComment.ArticleId == articleId
            //                           select new ArticleComment(tempArticleComment.ArticleId,
            //                               tempArticleComment.ArticleId,
            //                               tempArticleComment.Content,
            //                               tempArticleComment.PostedDate,
            //                              tempArticleComment.AuthorId)).ToList();
            //    }
            //}
            return articleComments;
        }
        public static bool DeleteAllCommentsByArticleId(int articleId)
        {
            AgriMarketDataClassesDataContext db = new AgriMarketDataClassesDataContext();
            List<Tbl_Comment> articleComments = (from tempArticletComment in db.Tbl_Comments
                      where tempArticletComment.ArticleId == articleId
                      select tempArticletComment).ToList();
            //if (articleComments.Count == 0)
            //{
            //    List<Tbl_Finished_Comment> finishedArticleComments = (from tempArticletComment in db.Tbl_Finished_Comments
            //                      where tempArticletComment.ArticleId == articleId
            //                      select tempArticletComment).ToList();
            //    if (finishedArticleComments.Count == 0)
            //    {
            //        List<Tbl_Long_Comment> LongArticleComments = (from tempArticletComment in db.Tbl_Long_Comments
            //                                                      where tempArticletComment.ArticleId == articleId
            //                                                      select tempArticletComment).ToList();
            //        db.Tbl_Long_Comments.DeleteAllOnSubmit(LongArticleComments);
            //    }
            //    else
            //    {
            //        db.Tbl_Finished_Comments.DeleteAllOnSubmit(finishedArticleComments);
            //    }
            //}
            //else
            //{
            //    db.Tbl_Comments.DeleteAllOnSubmit(articleComments);
            //}
            db.Tbl_Comments.DeleteAllOnSubmit(articleComments);
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
        public Tbl_Comment ToTbl_Comment()
        {
            Tbl_Comment result = new Tbl_Comment() {
                CommentId=this.commentId,
                ArticleId=this.articleId,
                Content=this.content,
                PostedDate=this.postedDate,
                AuthorId=this.authorId
            };
            return result;
        }
        //public Tbl_Long_Comment ToTbl_Long_Comment()
        //{
        //    Tbl_Long_Comment result = new Tbl_Long_Comment()
        //    {
        //        CommentId = this.commentId,
        //        ArticleId = this.articleId,
        //        Content = this.content,
        //        PostedDate = this.postedDate,
        //        AuthorId = this.authorId
        //    };
        //    return result;
        //}
        //public Tbl_Finished_Comment ToTbl_Finished_Comment()
        //{
        //    Tbl_Finished_Comment result = new Tbl_Finished_Comment()
        //    {
        //        CommentId = this.commentId,
        //        ArticleId = this.articleId,
        //        Content = this.content,
        //        PostedDate = this.postedDate,
        //        AuthorId = this.authorId
        //    };
        //    return result;
        //}
        public static List<ArticleComment> GetNFirstArticleComments(int articleId,int startedArticleCommentId, int count)
        {
            List<ArticleComment> articleCommentsList = ArticleComment.GetAllCommentByArticleId(articleId);
            List<ArticleComment> result = (from tempArticleComment in articleCommentsList
                                           where tempArticleComment.commentId > startedArticleCommentId
                                           orderby tempArticleComment.postedDate
                                           select tempArticleComment).Take(count).ToList();
            return result;
        }
    }
}